// VBConversions Note: VB project level imports
using System.Diagnostics;
using System;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using Microsoft.VisualBasic;
using System.Data;
using System.Collections.Generic;
using Microsoft.VisualBasic.Compatibility;
using System.Linq;
// End of VB project level imports

using System.Runtime.InteropServices;


namespace 喷码数据生成工具
{
	public partial class SystemSet
	{
		
#region Default Instance
		
		private static SystemSet defaultInstance;
		
		/// <summary>
		/// Added by the VB.Net to C# Converter to support default instance behavour in C#
		/// </summary>
public static SystemSet Default
		{
			get
			{
				if (defaultInstance == null)
				{
					defaultInstance = new SystemSet();
					defaultInstance.FormClosed += new FormClosedEventHandler(defaultInstance_FormClosed);
				}
				
				return defaultInstance;
			}
			set
			{
				defaultInstance = value;
			}
		}
		
		static void defaultInstance_FormClosed(object sender, FormClosedEventArgs e)
		{
			defaultInstance = null;
		}
		
#endregion
		//指定一个窗口的新父（在vb里使用：利用这个函数，vb可以多种形式支持子窗口。例如，可将控件从一个容器移至窗体中的另一个。用这个函数在窗体间移动控件是相当冒险的，但却不失为一个有效的办法。如真的这样做，请在关闭任何一个窗体之前，注意用SetParent将控件的父设回原来的那个）
		[DllImport("user32", EntryPoint = "SetParent", SetLastError = false, 
		CharSet = CharSet.Auto, ExactSpelling = false, 
		CallingConvention = CallingConvention.StdCall)]public static  extern int SetParent(int hWndChild, 
		int hWndNewParent);
		MSScriptControl.ScriptControl RunVBScript = new MSScriptControl.ScriptControl();
		ModifyAddress DataBaseSet = new ModifyAddress(false);
		private const string DescriptionSql1 = "SELECT  * FROM  ::fn_listextendedproperty (null, \'user\', \'dbo\', \'table\',\'";
		private const string DescriptionSql2 = "\', \'column\', default)";
		int SetPagesIndex = -1;
#region 构造函数
		public SystemSet()
		{
			
			// 此调用是 Windows 窗体设计器所必需的。
			InitializeComponent();
			
			//Added to support default instance behavour in C#
			if (defaultInstance == null)
				defaultInstance = this;
			
			// 在 InitializeComponent() 调用之后添加任何初始化。
			
		}
		public SystemSet(int SetIndex)
		{
			
			// 此调用是 Windows 窗体设计器所必需的。
			InitializeComponent();
			
			//Added to support default instance behavour in C#
			if (defaultInstance == null)
				defaultInstance = this;
			
			// 在 InitializeComponent() 调用之后添加任何初始化。
			SetPagesIndex = SetIndex;
		}
#endregion
		public void SystemSet_Load(object sender, System.EventArgs e)
		{
			SetParent(DataBaseSet.Handle.ToInt32(), SetPages.TabPages[0].Handle.ToInt32());
			DataBaseSet.Show();
			DataBaseSet.BackColor = Color.White;
			DataBaseSet.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
			DataBaseSet.WindowState = (System.Windows.Forms.FormWindowState) 2;
			//=================加载已保存的数据格式
			LB_CodeList.Items.Clear();
			SaveDataFormat.Items.Clear();
			LB_SearchTable.Items.Clear();
			if (CommFunction.SaveData.Setting != null)
			{
				for (var i = 0; i <= CommFunction.SaveData.Setting.Count - 1; i++)
				{
					SaveDataFormat.Items.Add(CommFunction.SaveData.Setting[i].SaveName);
				}
			}
			if (CommFunction.CheckData != null)
			{
				for (var i = 0; i <= CommFunction.CheckData.Count - 1; i++)
				{
					LB_CodeList.Items.Add(CommFunction.CheckData[System.Convert.ToInt32(i)].CheckName);
				}
			}
			if (CommFunction.SaveData.SearchTable != null)
			{
				for (var i = 0; i <= CommFunction.SaveData.SearchTable.Count - 1; i++)
				{
					LB_SearchTable.Items.Add(CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)].TableName);
				}
			}
			Btn_AddCode.Enabled = false;
			Btn_SaveCode.Enabled = false;
			RunVBScript.Language = "VBScript";
			if (SetPagesIndex >= 0 & SetPagesIndex < SetPages.TabPages.Count)
			{
				SetPages.SelectedIndex = SetPagesIndex;
			}
			ToolTip1.SetToolTip(TB_LoadDataVBS, "  1.必须是完整的函数过程；" + "\r\n" + "  2.函数有一个参数，参数类型为ArrayList；" + "\r\n" + "  3.函数返回值为字符型数据，并且以 | 符号分隔对应到数据表的每列数据；" + "\r\n" + "  4.返回值中数据段必须等于7。");
			ToolTip1.ToolTipTitle = "编码规则";
			ToolTip1.UseAnimation = true;
			ToolTip1.ShowAlways = true;
			ToolTip1.AutoPopDelay = 3000;
			//  ToolTip1.AutomaticDelay = 1
			for (var i = 0; i <= SetPages.TabPages.Count - 1; i++)
			{
				CommFunction.ChangeButtonFromFrm(SetPages.TabPages[i]);
			}
		}
		
		public void Btn_Remove_Click(System.Object sender, System.EventArgs e)
		{
			if (SaveDataFormat.SelectedIndex >= 0 & SaveDataFormat.SelectedIndex < SaveDataFormat.Items.Count)
			{
				string RemoveString = System.Convert.ToString(SaveDataFormat.Items[SaveDataFormat.SelectedIndex]);
				SaveDataFormat.Items.Remove(RemoveString);
				RemovedFormat.Items.Add(RemoveString);
			}
		}
		
		public void Btn_Recoevry_Click(System.Object sender, System.EventArgs e)
		{
			if (RemovedFormat.SelectedIndex >= 0 & RemovedFormat.SelectedIndex < RemovedFormat.Items.Count)
			{
				string Tmp = System.Convert.ToString(RemovedFormat.Items[RemovedFormat.SelectedIndex]);
				RemovedFormat.Items.Remove(Tmp);
				SaveDataFormat.Items.Add(Tmp);
			}
		}
		
		public void Btn_FormatSave_Click(System.Object sender, System.EventArgs e)
		{
			if (RemovedFormat.Items.Count > 0)
			{
				if (CommFunction.SaveData.Setting != null)
				{
					int SC = CommFunction.SaveData.Setting.Count;
					int RC = RemovedFormat.Items.Count;
					string tmp = "";
					int RemoveCount = 0;
					for (var i = 0; i <= RC - 1; i++)
					{
						tmp = RemovedFormat.Items[System.Convert.ToInt32(i)].ToString();
						for (var j = 0; j <= SC - 1; j++)
						{
							if (tmp.ToUpper() == CommFunction.SaveData.Setting[System.Convert.ToInt32(j)].SaveName.ToUpper)
							{
								CommFunction.SaveData.Setting.RemoveAt(System.Convert.ToInt32(j));
								Form1.Default.RemoveSetting(tmp);
								RemoveCount++;
								break;
							}
						}
					}
					if (Interaction.MsgBox("是否删除当前选中的数据格式多个数据！", (int) MsgBoxStyle.Question + MsgBoxStyle.YesNo, "询问") == MsgBoxResult.Yes)
					{
						Form1.Default.SaveSetting();
						RemovedFormat.Items.Clear();
						Interaction.MsgBox("数据保存成功，共删除：" + System.Convert.ToString(RemoveCount) + "条记录！", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示");
					}
				}
				
			}
		}
		
		public void Btn_RunCode_Click(System.Object sender, System.EventArgs e)
		{
			string Result = "";
			string gCode = "";
			try
			{
				gCode = getCheckFunction();
				RunVBScript.AddCode(gCode);
				Result = System.Convert.ToString(RunVBScript.Run(TB_CheckFunName.Text, TB_TestData.Text));
				CheckLength.Text = System.Convert.ToString(Result.Length);
				Interaction.MsgBox("测试结果为：" + Result, MsgBoxStyle.Information, "提示");
				Btn_AddCode.Enabled = true;
				Btn_SaveCode.Enabled = true;
			}
			catch (Exception ex)
			{
				Btn_AddCode.Enabled = false;
				Btn_SaveCode.Enabled = false;
				Interaction.MsgBox(ex.Message, (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "错误");
			}
		}
		
		public void Btn_AddCode_Click(System.Object sender, System.EventArgs e)
		{
			if (string.IsNullOrEmpty(TB_CheckName.Text) || string.IsNullOrEmpty(TB_CheckFunName.Text))
			{
				Interaction.MsgBox("校验方式名称和函数名称，不能为空！", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "提示");
			}
			return ;
//			if (Information.IsNumeric(CheckLength.Text) == false)
//			{
//				Interaction.MsgBox("校验码长度必须是数值型数据！", MsgBoxStyle.Critical, "提示");
//				}
//				return ;
//				if (Conversion.Val(CheckLength.Text) <= 0)
//				{
//					Interaction.MsgBox("校验码长度必须大于或等于1！", MsgBoxStyle.Critical, "提示");
//					}
//					return ;
//					CommFunction.CheckMode tmpCheckMode = new CommFunction.CheckMode();
//					tmpCheckMode.CheckName = TB_CheckName.Text;
//					tmpCheckMode.CheckFunName = TB_CheckFunName.Text;
//					tmpCheckMode.CheckLength = (int) CheckLength.Text;
//					tmpCheckMode.CheckDescription = CheckDescription.Text;
//					if (CommFunction.CheckData != null)
//					{
//						for (var i = 0; i <= CommFunction.CheckData.Count - 1; i++)
//						{
//							if (tmpCheckMode.CheckName.ToUpper() == CommFunction.CheckData[System.Convert.ToInt32(i)].CheckName.ToUpper)
//							{
//								Interaction.MsgBox("校验方式已经存在，请确定是否已经添加过此校验方式！", MsgBoxStyle.Critical, "错误");
//								}
//								return ;
//								if (tmpCheckMode.CheckFunName.ToUpper() == CommFunction.CheckData[System.Convert.ToInt32(i)].CheckFunName.ToUpper)
//								{
//									Interaction.MsgBox("校验方式函数名重复，请修改校验方式函数名！", MsgBoxStyle.Critical, "错误");
//									}
//									return ;
//									}
//									}
//									tmpCheckMode.CheckCode = getCheckFunction();
//									tmpCheckMode.CheckCode = CheckCode_Script.Text;
//									if (CommFunction.SaveData.CheckNumberMode != null)
//									{
//										CommFunction.SaveData.CheckNumberMode.Add(tmpCheckMode);
//										}
//										else
//										{
//											CommFunction.SaveData.CheckNumberMode = new List<CommFunction.CheckMode>();
//											CommFunction.SaveData.CheckNumberMode.Add(tmpCheckMode);
//											}
//											LB_CodeList.Items.Add(tmpCheckMode.CheckName);
//											Form1.Default.SaveSetting();
//											Form1.Default.ReLoadCheckMode();
//											Interaction.MsgBox("【" + tmpCheckMode.CheckName + "】校验方式已经保存，请先测试准确后再使用！", MsgBoxStyle.Information, "提示");
										}
										private string getCheckFunction()
										{
											string FunName = TB_CheckFunName.Text;
											string FunCode = CheckCode_Script.Text;
											string[] LinesCode = null;
											string tmpName = "";
											bool NameExist = false;
											bool EndCode = false;
											string Param = "";
											int StartLine = 0;
											int EndLine = 0;
											string Result = "";
											int CodeStartLine = -1;
											string TmpCode = "";
											LinesCode = FunCode.Split("\r\n");
											for (var i = 0; i <= LinesCode.Length - 1; i++)
											{
												TmpCode = LinesCode[(int) i].Trim();
												if (string.IsNullOrEmpty(TmpCode) == false && CodeStartLine < 0)
												{
													CodeStartLine = System.Convert.ToInt32(i);
												}
												if (TmpCode.ToUpper().IndexOf("function ".ToUpper()) + 1 > 0)
												{
													int a = TmpCode.IndexOf("(");
													int c = TmpCode.IndexOf(")");
													int b = TmpCode.ToUpper().IndexOf("function ".ToUpper());
													if (a > 8)
													{
														tmpName = TmpCode.Substring(b + 9, a - b - 9).Trim();
														if (b - a + 1 > 0)
														{
															Param = TmpCode.Substring(a + 1, b - a + 1);
														}
														StartLine = System.Convert.ToInt32(i);
														NameExist = true;
													}
												}
												if (TmpCode.ToUpper().IndexOf("end ".ToUpper()) + 1 > 0 && (TmpCode.ToUpper().IndexOf(" function".ToUpper()) + 1 ) > 0)
												{
													if (TmpCode.Replace(" ", "").ToUpper().Trim() == "endfunction".ToUpper())
													{
														EndCode = true;
														EndLine = System.Convert.ToInt32(i);
														break;
													}
												}
											}
											// MsgBox(CodeStartLine & "|" & StartLine)
											// If NameExist And EndCode Then If Param = "" Or Param.IndexOf(",") >= 0 Then MsgBox("参数和校验需求不匹配！" & Param, MsgBoxStyle.Critical, "错误") : Return ""
											//If NameExist = False Or EndCode = False Then MsgBox("函数过程不完整！", MsgBoxStyle.Critical, "编码错误") : Return ""
											if (NameExist && EndCode)
											{
												if (FunName.ToUpper().Trim() != tmpName.ToUpper().Trim() && StartLine == CodeStartLine)
												{
													throw (new Exception("函数名与设定不匹配，请重新输入函数名！" + FunName + "|" + tmpName));
												}
												return "";
//												if (FunName.ToUpper().Trim() == tmpName.ToUpper().Trim() && string.IsNullOrEmpty(FunName) == true)
//												{
//													throw (new Exception("未设置函数名，请修改源码和设置！"));
//													}
//													return "";
												}
												if (CodeStartLine != StartLine & StartLine > CodeStartLine)
												{
													Result = "function " + FunName + "(a)";
													for (var i = 0; i <= StartLine - 1; i++)
													{
														Result = Result + "\r\n" + LinesCode[(int) i];
													}
													Result = Result + "\r\n" + "end function";
													for (var i = StartLine; i <= LinesCode.Length - 1; i++)
													{
														Result = Result + "\r\n" + LinesCode[(int) i];
													}
													return Result;
												}
												else
												{
													if (EndLine > StartLine)
													{
														for (var i = 0; i <= LinesCode.Length - 1; i++)
														{
															if (Result != "")
															{
																Result = Result + "\r\n" + LinesCode[(int) i];
															}
															else
															{
																Result = LinesCode[(int) i];
															}
														}
														return Result;
													}
													else
													{
														return "function " + FunName + "(a)" + "\r\n" + FunCode + "\r\n" + "end function";
													}
												}
											}
											
											public void Btn_SaveCode_Click(System.Object sender, System.EventArgs e)
											{
												string tmp = TB_CheckName.Text;
												string sc = TB_CheckFunName.Text;
												int ObjIndex = -1;
												CommFunction.CheckMode ModyfyCheck = new CommFunction.CheckMode();
												if (CommFunction.SaveData.CheckNumberMode != null)
												{
													for (var i = 0; i <= CommFunction.SaveData.CheckNumberMode.Count - 1; i++)
													{
														if (CommFunction.SaveData.CheckNumberMode[System.Convert.ToInt32(i)].CheckName.ToUpper == tmp.ToUpper() && CommFunction.SaveData.CheckNumberMode[System.Convert.ToInt32(i)].CheckFunName.ToUpper == sc.ToUpper())
														{
															ObjIndex = System.Convert.ToInt32(i);
															ModyfyCheck = CommFunction.SaveData.CheckNumberMode[System.Convert.ToInt32(i)];
														}
													}
												}
												if (ObjIndex >= 0)
												{
													ModyfyCheck.CheckName = TB_CheckName.Text;
													ModyfyCheck.CheckLength = (int) CheckLength.Text;
													ModyfyCheck.CheckFunName = TB_CheckFunName.Text;
													ModyfyCheck.CheckDescription = CheckDescription.Text;
													ModyfyCheck.CheckCode = CheckCode_Script.Text;
													CommFunction.SaveData.CheckNumberMode[ObjIndex] = ModyfyCheck;
													Form1.Default.SaveSetting();
													Interaction.MsgBox("【" + ModyfyCheck.CheckName + "]校验方式修改成功，请注意测试校验方式的准确性！", MsgBoxStyle.Information, "提示");
												}
												else
												{
													Interaction.MsgBox("当前选中校验方式为系统内置函数不能修改！", MsgBoxStyle.Information, "提示");
												}
											}
											
											public void LB_CodeList_DoubleClick(object sender, System.EventArgs e)
											{
												try
												{
													string SelectedString = System.Convert.ToString(LB_CodeList.Items[LB_CodeList.SelectedIndex]);
													CommFunction.CheckMode selectedMode = null;
													if (string.IsNullOrEmpty(SelectedString) == false)
													{
														for (var i = 0; i <= CommFunction.CheckData.Count - 1; i++)
														{
															if (SelectedString.ToUpper() == CommFunction.CheckData[System.Convert.ToInt32(i)].CheckName.ToUpper)
															{
																selectedMode = CommFunction.CheckData[System.Convert.ToInt32(i)];
																break;
															}
														}
													}
													try
													{
														TB_CheckName.Text = selectedMode.CheckName;
														TB_CheckFunName.Text = selectedMode.CheckFunName;
														CheckDescription.Text = selectedMode.CheckDescription;
														CheckCode_Script.Text = selectedMode.CheckCode;
														CheckLength.Text = System.Convert.ToString(selectedMode.CheckLength);
													}
													catch (Exception)
													{
														
													}
												}
												catch
												{
												}
											}
											
											public void LB_CodeList_SelectedIndexChanged(System.Object sender, System.EventArgs e)
											{
												
											}
											
											public void Btn_CheckDel_Click(System.Object sender, System.EventArgs e)
											{
												string tmp = TB_CheckName.Text;
												string sc = TB_CheckFunName.Text;
												int ObjIndex = -1;
												CommFunction.CheckMode ModyfyCheck;
												if (CommFunction.SaveData.CheckNumberMode != null)
												{
													for (var i = 0; i <= CommFunction.SaveData.CheckNumberMode.Count - 1; i++)
													{
														if (CommFunction.SaveData.CheckNumberMode[System.Convert.ToInt32(i)].CheckName.ToUpper == tmp.ToUpper() && CommFunction.SaveData.CheckNumberMode[System.Convert.ToInt32(i)].CheckFunName.ToUpper == sc.ToUpper())
														{
															ObjIndex = System.Convert.ToInt32(i);
															ModyfyCheck = CommFunction.SaveData.CheckNumberMode[System.Convert.ToInt32(i)];
														}
													}
												}
												if (ObjIndex >= 0)
												{
													if (Interaction.MsgBox("是否删除当前选中的校验方式【" + tmp + "】？", (int) MsgBoxStyle.Question + MsgBoxStyle.YesNo, "询问") == MsgBoxResult.Yes)
													{
														CommFunction.SaveData.CheckNumberMode.RemoveAt(ObjIndex);
														for (var i = 0; i <= LB_CodeList.Items.Count - 1; i++)
														{
															if (LB_CodeList.Items[System.Convert.ToInt32(i)].ToString().ToUpper() == tmp.ToUpper())
															{
																LB_CodeList.Items.Remove(i);
															}
															break;
														}
														Form1.Default.SaveSetting();
														ClearAllText(TB_CheckFunName.Parent);
														Interaction.MsgBox("数据删除成功！", MsgBoxStyle.Information, "提示");
													}
												}
												else
												{
													Interaction.MsgBox("该校验方式为系统内置函数不能删除！", MsgBoxStyle.Information, "提示");
												}
											}
											private void ClearAllText(Control Parent)
											{
												Type tp;
												foreach (object item in Parent.Controls)
												{
													tp = item.GetType();
													if (tp.ToString().ToUpper() == "System.Windows.Forms.TextBox".ToUpper())
													{
														item.text = "";
													}
												}
											}
											public void Btn_DataBaseAdd_Click(System.Object sender, System.EventArgs e)
											{
												string TableName = TB_SearchTable.Text;
												if (string.IsNullOrEmpty(TB_SearchTable.Text))
												{
													Interaction.MsgBox("搜索数据的表明不能为空！", MsgBoxStyle.Critical, "错误");
												}
												return ;
//												if (string.IsNullOrEmpty(TB_LoadDataSql.Text))
//												{
//													Interaction.MsgBox("加载数据库的Sql语句不能为空！", MsgBoxStyle.Critical, "错误");
//													}
//													return ;
//													if (string.IsNullOrEmpty(TB_SearchSql.Text))
//													{
//														Interaction.MsgBox("搜索数据库的Sql语句不能为空！", MsgBoxStyle.Critical, "错误");
//														}
//														return ;
//														if (string.IsNullOrEmpty(TB_LoadDataVBS.Text))
//														{
//															Interaction.MsgBox("加载数据到生产数据显示表的VBS脚本代码不能为空，在添加前最好先测试代码！", MsgBoxStyle.Critical, "错误");
//															}
//															return ;
//															if (CommFunction.SaveData.SearchTable != null)
//															{
//																for (var i = 0; i <= CommFunction.SaveData.SearchTable.Count - 1; i++)
//																{
//																	if (CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)].TableName.ToUpper == TableName.ToUpper() && CommFunction.SaveData.DataBase.ToUpper() == CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)].DataBaseName.ToUpper)
//																	{
//																		Interaction.MsgBox("要搜索的数据库表已经存在，请选择其他表！", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示");
//																		return ;
//																		}
//																		}
//																		}
//																		CommFunction.SearchData tmpData = new CommFunction.SearchData();
//																		tmpData.DataBaseName = CommFunction.SaveData.DataBase;
//																		tmpData.TableName = TableName;
//																		tmpData.SearchSql = TB_SearchSql.Text;
//																		tmpData.LoadDataVBS = TB_LoadDataVBS.Text;
//																		tmpData.DownLoadDataSql = TB_LoadDataSql.Text;
//																		tmpData.DescriptionSql = TB_DescriptionSql.Text;
//																		if (CommFunction.SaveData.SearchTable == null)
//																		{
//																			CommFunction.SaveData.SearchTable = new List<CommFunction.SearchData>();
//																			}
//																			CommFunction.SaveData.SearchTable.Add(tmpData);
//																			LB_SearchTable.Items.Add(tmpData.TableName);
//																			Form1.Default.SaveSetting();
//																			Interaction.MsgBox("数据保存成功！", MsgBoxStyle.Information, "提示");
																		}
																		
																		public void Btn_DataBaseRun_Click(System.Object sender, System.EventArgs e)
																		{
																			// ClearAllText(Btn_DataBaseRun.Parent)
																			
																			try
																			{
																				RunVBScript.AddCode(TB_LoadDataVBS.Text);
																				RunVBScript.Run(CommFunction.getVbsSubOrFunName(TB_LoadDataVBS.Text));
																			}
																			catch (Exception ex)
																			{
																				MessageBox.Show(ex.Message);
																			}
																		}
																		
																		public void LB_SearchTable_DoubleClick(object sender, System.EventArgs e)
																		{
																			string tmp = "";
																			CommFunction.SearchData tmpData = new CommFunction.SearchData();
																			try
																			{
																				tmp = LB_SearchTable.Items[LB_SearchTable.SelectedIndex].ToString();
																				if (CommFunction.SaveData.SearchTable != null)
																				{
																					for (var i = 0; i <= CommFunction.SaveData.SearchTable.Count - 1; i++)
																					{
																						if (CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)].TableName.ToUpper == tmp.ToUpper())
																						{
																							tmpData = CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)];
																						}
																					}
																				}
																				
																				// .DataBaseName = SaveData.DataBase
																				TB_SearchTable.Text = tmpData.TableName;
																				TB_SearchSql.Text = tmpData.SearchSql;
																				TB_LoadDataVBS.Text = tmpData.LoadDataVBS;
																				TB_LoadDataSql.Text = tmpData.DownLoadDataSql;
																				TB_DescriptionSql.Text = tmpData.DescriptionSql;
																			}
																			catch (Exception ex)
																			{
																				Interaction.MsgBox(ex.Message, MsgBoxStyle.Critical, "错误");
																			}
																		}
																		
																		public void LB_SearchTable_SelectedIndexChanged(System.Object sender, System.EventArgs e)
																		{
																			
																		}
																		
																		public void TB_SearchTable_TextChanged(System.Object sender, System.EventArgs e)
																		{
																			TB_DescriptionSql.Text = DescriptionSql1 + TB_SearchTable.Text + DescriptionSql2;
																			if (string.IsNullOrEmpty(TB_LoadDataVBS.Text))
																			{
																				TB_LoadDataVBS.AppendText("Function " + TB_SearchTable.Text + "(Param,TB_Prefix,TB_Suffix)");
																				TB_LoadDataVBS.AppendText("\r\n" + "\r\n" + "End Function");
																			}
																			else
																			{
																				TB_LoadDataVBS.Text = ChangeFunName(TB_LoadDataVBS.Text, TB_SearchTable.Text);
																			}
																		}
																		
																		public void Btn_SearchData_Click(System.Object sender, System.EventArgs e)
																		{
																			string tmpName = TB_SearchTable.Text;
																			CommFunction.SearchData TmpData = new CommFunction.SearchData();
																			if (CommFunction.SaveData.SearchTable != null)
																			{
																				for (var i = 0; i <= CommFunction.SaveData.SearchTable.Count - 1; i++)
																				{
																					if (tmpName.ToUpper() == CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)].TableName.ToUpper)
																					{
																						if (Interaction.MsgBox("是否修改该表搜索下载数据的设置？", (int) MsgBoxStyle.Question + MsgBoxStyle.OkCancel, "询问") == MsgBoxResult.Ok)
																						{
																							TmpData.TableName = TB_SearchTable.Text;
																							TmpData.SearchSql = TB_SearchSql.Text;
																							TmpData.LoadDataVBS = TB_LoadDataVBS.Text;
																							TmpData.DownLoadDataSql = TB_LoadDataSql.Text;
																							TmpData.DescriptionSql = TB_DescriptionSql.Text;
																							TmpData.DataBaseName = CommFunction.SaveData.DataBase;
																							CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)] = TmpData;
																							Form1.Default.SaveSetting();
																							Interaction.MsgBox("修改数据保存成功！", MsgBoxStyle.Information, "提示");
																						}
																					}
																				}
																			}
																		}
																		
																		private void TB_LoadDataVBS_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
																		{
																			ToolTip1.Show("  1.必须是完整的函数过程；" + "\r\n" + "  2.函数有一个参数，参数类型为ArrayList；" + "\r\n" + "  3.函数返回值为字符型数据，并且以 | 符号分隔对应到数据表的每列数据；" + "\r\n" + "  4.返回值中数据段必须等于7。", this);
																		}
																		private string ChangeFunName(string Code, string Name)
																		{
																			string[] fun = null;
																			string Value = "";
																			bool Fstart = false;
																			string Parame = "";
																			bool EndCode = false;
																			bool NR = false;
																			fun = Code.Split('\n');
																			for (var i = 0; i <= fun.Length - 1; i++)
																			{
																				//  If fun(i).Trim <> "" Then
																				if ((fun[(int) i].ToUpper()).IndexOf("Function ".ToUpper()) + 1 > 0)
																				{
																					Fstart = true;
																					int a = System.Convert.ToInt32(fun[(int) i].IndexOf("("));
																					int b = System.Convert.ToInt32(fun[(int) i].IndexOf(")"));
																					if (b - a > 1)
																					{
																						Parame = System.Convert.ToString(fun[(int) i].Substring(a + 1, b - a - 1));
																						string[] n = Parame.Split(',');
																						if (n.Length != 3)
																						{
																							Parame = "Param,TB_Prefix,TB_Suffix";
																						}
																					}
																					else
																					{
																						Parame = "Param,TB_Prefix,TB_Suffix";
																					}
																					if (string.IsNullOrEmpty(Value))
																					{
																						Value = "Function " + Name + "(" + Parame + ")";
																					}
																					else
																					{
																						Value += "\r\n" + "Function " + Name + "(" + Parame + ")";
																					}
																				}
																				else if (fun[(int) i].ToUpper().IndexOf("End ".ToUpper()) < 0 && fun[(int) i].ToUpper().IndexOf(" Function".ToUpper()) < 0)
																				{
																					if (string.IsNullOrEmpty(Value))
																					{
																						Value = fun[(int) i];
																					}
																					else
																					{
																						Value += "\r\n" + fun[(int) i];
																					}
																					if (fun[(int) i].Trim() != "")
																					{
																						NR = true;
																					}
																					EndCode = false;
																				}
																				else if (fun[(int) i].ToUpper().IndexOf("End ".ToUpper()) >= 0 && fun[(int) i].ToUpper().IndexOf(" Function".ToUpper()) > 0)
																				{
																					if (NR == true)
																					{
																						Value += "\r\n" + fun[(int) i];
																					}
																					else
																					{
																						Value += "\r\n" + "\r\n" + "\'在此处添加函数内容" + "\r\n" + fun[(int) i];
																					}
																					EndCode = true;
																				}
																			}
																			if (EndCode == false)
																			{
																				if (NR == false)
																				{
																					Value += "\r\n" + "\r\n" + "\'在此处添加函数内容" + "\r\n" + "End Function";
																				}
																			}
																			return Value;
																		}
																		
																		public void Btn_ModyfyFormat_Click(System.Object sender, System.EventArgs e)
																		{
																			string tmp = "";
																			if (SaveDataFormat.SelectedIndex < 0)
																			{
																				Interaction.MsgBox("请先选中要修改保存名称的数据！", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "提示");
																				return ;
																			}
																			else
																			{
																				tmp = System.Convert.ToString(SaveDataFormat.Items[SaveDataFormat.SelectedIndex]);
																				int SC = CommFunction.SaveData.Setting.Count - 1;
																				int FindIndex = -1;
																				CommFunction.SaveSetting FindData = null;
																				bool HasFind = false;
																				for (var i = 0; i <= SC; i++)
																				{
																					if (tmp.ToUpper() == CommFunction.SaveData.Setting[i].SaveName.ToUpper)
																					{
																						FindIndex = System.Convert.ToInt32(i);
																						FindData = CommFunction.SaveData.Setting[i];
																						break;
																					}
																				}
																				if (FindIndex >= 0)
																				{
																					string ModifyName = RemedyCode.ShowInput(this, "请输入修改内容", FindData.SaveName, false);
																					bool HasExist = false;
																					if (string.IsNullOrEmpty(ModifyName))
																					{
																						return ;
																					}
																					if (ModifyName != "")
																					{
																						for (var i = 0; i <= SC; i++)
																						{
																							if (ModifyName.ToUpper() == CommFunction.SaveData.Setting[i].SaveName.ToUpper)
																							{
																								HasExist = true;
																								break;
																							}
																						}
																					}
																					if (HasExist)
																					{
																						Interaction.MsgBox("输入的内容已经存在，请重新输入！", MsgBoxStyle.Information, "提示");
																					}
																					else
																					{
																						FindData.SaveName = ModifyName;
																						CommFunction.SaveData.Setting[FindIndex] = FindData;
																						SaveDataFormat.Items[SaveDataFormat.SelectedIndex] = FindData.SaveName;
																						Form1.Default.SaveSetting();
																						Form1.Default.ModifySetting(tmp, FindData.SaveName);
																						Interaction.MsgBox("数据修改成功！", MsgBoxStyle.Information, "提示");
																					}
																				}
																			}
																		}
																		
																		public void Btn_DeleteTable_Click(System.Object sender, System.EventArgs e)
																		{
																			string tmp = "";
																			try
																			{
																				tmp = System.Convert.ToString(LB_SearchTable.Items[LB_SearchTable.SelectedIndex]);
																			}
																			catch
																			{
																			}
																			if (LB_SearchTable.SelectedIndex < 0)
																			{
																				Interaction.MsgBox("请先选择要删除的数据表！", MsgBoxStyle.Information, "提示");
																			}
																			return ;
//																			for (var i = 0; i <= CommFunction.SaveData.SearchTable.Count - 1; i++)
//																			{
//																				if (tmp.ToUpper() == CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)].TableName.ToUpper)
//																				{
//																					LB_SearchTable.Items.Remove(tmp);
//																					CommFunction.SaveData.SearchTable.RemoveAt(System.Convert.ToInt32(i));
//																					Interaction.MsgBox("数据表已从本地删除成功！", MsgBoxStyle.OkOnly, "提示");
//																					return ;
//																					}
//																					}
																				}
																			}
																		}
